package com.huage_07.lesson_04;

import javax.swing.*;
import java.io.*;

public class CopyFoldersDemo {
    public static void main(String[] args) throws IOException{
        File srcFolder = new File("D:\\document\\java\\CopyTest");
        String folderName = srcFolder.getName();
        File destFolder = new File("class1\\src\\com\\huage_07\\lesson_04",folderName);

        if (!destFolder.exists()){
            destFolder.mkdir();
        }

        MkFoders(srcFolder,destFolder);
    }
    public static void MkFoders(File srcFolder,File destFolder)throws IOException {
        File[] files = srcFolder.listFiles();
        for (File file : files){
            String srcName = file.getName();
            if(!file.isDirectory()){
                File dfile = new File(destFolder,srcName);
                copyFile(file,dfile);
            }else {
                File sFolder = new File(srcFolder,srcName);
                File dFolder = new File(destFolder,srcName);
                dFolder.mkdir();
                MkFoders(sFolder,dFolder);
            }
        }

    }
    public static void copyFile(File srcFlie,File destFile){
        try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream(srcFlie));
             BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(destFile));){
            byte[] bytes = new byte[1024];
            int len;
            while ((len=bis.read(bytes))!=-1){
                bos.write(bytes,0,len);
            }

        }catch (IOException e){
            e.printStackTrace();
        }
    }
}
